package com.adpter;

import java.util.ArrayList;
import java.util.List;

import u.aly.at;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.method.ServerPath;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;
import com.matang.R;
import com.modle.Attach;
import com.modle.PyqUser;
import com.util.AppUtil;
import com.util.JsonUtil1;
import com.view.NonScrollGridView;

public class PyqAdapter extends BaseAdapter {
	ArrayList<PyqUser> dataList = new ArrayList<PyqUser>();
	Context context;
	BitmapUtils bitmapUtils;
	public PyqAdapter( Context context) {
		super();
		if (bitmapUtils == null) {
			bitmapUtils = new BitmapUtils(context);
		}
		this.context = context;

	}

	@Override
	public int getCount() {
		return dataList.size();
	}

	@Override
	public Object getItem(int position) {
		return dataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int pos, View view, ViewGroup parent) {
		ViewHolder vh = null;
		if (view == null) {
			view = LayoutInflater.from(context).inflate(
					R.layout.item_pyq, null);
			vh = new ViewHolder();
			vh.tvName = (TextView) view.findViewById(R.id.tv_name);
			vh.tvContent = (TextView) view.findViewById(R.id.tv_msg);
			vh.imghead = (ImageView) view.findViewById(R.id.img_user);
			vh.tpgrid = (NonScrollGridView) view.findViewById(R.id.grid_img_room);
			vh.tvTime =  (TextView) view.findViewById(R.id.tv_time);
		
			view.setTag(vh);
		} else {
			vh = (ViewHolder) view.getTag();

		}


		PyqUser bs =dataList.get(pos);
		vh.tvContent.setText(bs.getContent());
		vh.tvName.setText(bs.getNickname());
		long publish = bs.getPublishAt();
		long ct = System.currentTimeMillis();
	
		int fz = (int) ((ct - publish*1000)/1000/60);
		String mtime =  fz+"分钟前";
		if (fz > 60 && fz <1440) {
			
			mtime = (fz/60)+"小时前";
		}else if (fz > 1440 && fz <43200) {
			mtime = (fz/60/24)+"天前";
		}
		
		vh.tvTime.setText(mtime);
		String attachments = bs.getAttachments();
		if (attachments !=null && !"[]".equals(attachments)) {

			List<Attach> attachs = JsonUtil1.parseArray(attachments,Attach.class);
			List<Attach> data = new ArrayList<Attach>();
			data.clear();
			if (attachments != null) {
				PyqGridViewAdapter gridViewAdapter = null;
				if (gridViewAdapter == null) {
					gridViewAdapter = new PyqGridViewAdapter(context);
				}
				int tg = -1;
				for (Attach attach : attachs) {
					if (attach.getThumb().contains("2015")) {
						data.add(attach);
					}
					Log.i("test", "图片路径=="+attach.getThumb());
				}
				
				vh.tpgrid.setAdapter(gridViewAdapter);
				vh.tpgrid.setVisibility(View.VISIBLE);
				gridViewAdapter.addDatas(data);
				
				if (attachs.size() != 0) {
			
					
				}
				
			}

		}else {

			vh.tpgrid.setVisibility(View.GONE);
		}
		
		String thumb = ServerPath.SERVER+bs.getAvatar();
		//bitmapUtils.display(vh.imghead, thumb);
		bitmapUtils.display(vh.imghead, thumb, new BitmapLoadCallBack<View>() {

			public void onLoadCompleted(View container, String uri,
					Bitmap bitmap, BitmapDisplayConfig config,
					BitmapLoadFrom from) {
				((ImageView)container).setImageBitmap(AppUtil.getRoundedCornerBitmap(bitmap));
				
			}

			@Override
			public void onLoadFailed(View container, String uri,
					Drawable drawable) {
				((ImageView)container).setImageResource(R.drawable.default_avatar);
				
			}
		});
  


		return view;
	}

	class ViewHolder {
		ImageView imghead;
	//	ImageView imgTp;
		TextView tvName;
		TextView tvContent;
		TextView tvTime;
		NonScrollGridView tpgrid;
	}
	public void addData(List<PyqUser> dataLists){
		dataList.addAll(dataLists);
		notifyDataSetChanged();
	}
	public void clearData(){
		dataList.clear();
		notifyDataSetChanged();
	}
}